package com.bookquery.service.impl;

import com.bookquery.dao.impl.BookQueryDaoImpl;
import com.bookquery.entity.BookQuery;
import com.bookquery.service.BookQueryService;
import com.bookquery.utils.DatabaseUtil;

import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 刘俊伟
 * 图书查询业务层实现类
 */
public class BookQueryServiceImpl implements BookQueryService {
    /**
     * 按条件组合查询图书信息
     *
     * @param bookName   图书名称
     * @param bookAuthor 图书作者
     * @param bookType   图书类别
     * @return 查询结果
     */
    @Override
    public List<BookQuery> queryBook(String bookName, String bookAuthor, String bookType) {
        Connection connection =null;
        try {
            connection = DatabaseUtil.getConnection();
            return new BookQueryDaoImpl(connection).queryBook(bookName,bookAuthor,bookType);
        } catch (SQLException | NamingException e) {
            e.printStackTrace();
        }finally {
            DatabaseUtil.closeAll(connection,null,null);
        }
        return null;
    }

    /**
     * 根据图书id查询特定图书信息
     *
     * @param bookId
     * @return
     */
    @Override
    public BookQuery queryBookById(int bookId) {
        Connection connection =null;
        try {
            connection = DatabaseUtil.getConnection();
            return new BookQueryDaoImpl(connection).queryBookById(bookId);
        } catch (SQLException | NamingException e) {
            e.printStackTrace();
        }finally {
            DatabaseUtil.closeAll(connection,null,null);
        }
        return null;
    }
}
